package CompareScheme;

import accessstruct.Tool;
import accessstruct.parser.ParserUtils;
import it.unisa.dia.gas.jpbc.Element;
import it.unisa.dia.gas.jpbc.Pairing;
import it.unisa.dia.gas.plaf.jpbc.pairing.PairingFactory;
/**
 * @Classname paper1
 * @Description
 * cition ：Ma J, Li T, Cui J, et al.
 * Attribute-Based Secure Announcement Sharing Among Vehicles Using Blockchain[J].
 * IEEE Internet of Things Journal, 2021, 8(13):10873-10883.
 * DOI:10.1109/JIOT.2021.3050802..
 * @Created by kevin
 * //模拟计算过程
 */
public class paper1 {
    public static long Setup(){
        Pairing bp = PairingFactory.getPairing("a.properties");//
        long start = System.currentTimeMillis();

        Element P = bp.getG1().newRandomElement().getImmutable();//
        Element Q = bp.getG1().newRandomElement().getImmutable();//
        Element a = bp.getZr().newRandomElement().getImmutable();//
        Element b = bp.getZr().newRandomElement().getImmutable();//
        Element epp_ab = bp.pairing(P,P).powZn(a).powZn(b);
        Element aP = P.mul(a).getImmutable();//
        Element aQ = a.mul(Q).getImmutable();//
        long end = System.currentTimeMillis();
        return end-start;
    }
    public static long KeyGen(int attNum) throws Exception{
        Pairing bp = PairingFactory.getPairing("a.properties");//
        String att = "1024";
        String t = "sub";
        Element P = bp.getG1().newRandomElement().getImmutable();//
        Element Q = bp.getG1().newRandomElement().getImmutable();//
        Element a = bp.getZr().newRandomElement().getImmutable();//
        Element b = bp.getZr().newRandomElement().getImmutable();//
        Element epp_ab = bp.pairing(P,P).powZn(a).powZn(b);
        Element aP = a.mul(P).getImmutable();//
        Element aQ = a.mul(Q).getImmutable();//
        long start = System.currentTimeMillis();
        Element alpha = bp.getZr().newRandomElement().getImmutable();//
        Element beta  = bp.getZr().newRandomElement().getImmutable();//
        Element D  = P.mul(a.add(beta.mul(b))).getImmutable();//
        Element D1  = P.mul(beta).add(alpha.mul(Q)).getImmutable();//
        Element D2  = P.mul(alpha).getImmutable();//
        for(int i = 0 ;i<attNum;i++){
            Element rj = bp.getZr().newRandomElement().getImmutable();//
            byte[] attHash = Tool.sha1(att);
            Element Si = bp.getG1().newElementFromHash(attHash, 0, attHash.length).getImmutable();
            byte[] attHash1 = Tool.sha1(t);
            Element ht = bp.getG1().newElementFromHash(attHash1, 0, attHash1.length).getImmutable();
            Element Di  = P.mul(b.mul(beta)).add(rj.mul(Si.add(ht))).getImmutable();//
            Element rjP  = P.mul(rj).getImmutable();//
        }
        long end = System.currentTimeMillis();
        return end-start;
    }
    public static long Encrypt(int attNum)throws Exception{
        Pairing bp = PairingFactory.getPairing("a.properties");//
        String att = "1024";
        String t = "sub";
        Element P = bp.getG1().newRandomElement().getImmutable();//
        Element Q = bp.getG1().newRandomElement().getImmutable();//
        Element a = bp.getZr().newRandomElement().getImmutable();//
        Element b = bp.getZr().newRandomElement().getImmutable();//
        Element epp_ab = bp.pairing(P,P).powZn(a).powZn(b);
        Element aP = a.mul(P).getImmutable();//
        Element bQ = b.mul(Q).getImmutable();//
        Element temp = bp.getG1().newRandomElement().getImmutable();//
        //rsu加密
        Element s  = bp.getZr().newRandomElement().getImmutable();//
        byte[] attHash = Tool.sha1(att);

        Element Si = bp.getG1().newElementFromHash(attHash, 0, attHash.length).getImmutable();
        Element C3_ = s.mul(P).mul(b).getImmutable();//
        Element C4_ = s.mul(Q).mul(b).getImmutable();//
        for(int i = 0 ;i<attNum;i++ ){

            Element Cy = a.mul(P).getImmutable();//
            Element Cy1 = a.mul(Si).getImmutable();//
        }
//        Element C4_ = s.mul(Q).mul(b).getImmutable();/

        long start = System.currentTimeMillis();
        Element rs  = bp.getZr().newRandomElement().getImmutable();//
        Element phi  = bp.getZr().newRandomElement().getImmutable();//
        Element C1 = rs.mul(bp.pairing(P,P).powZn(a).powZn(phi));
        Element C2 = phi.mul(P).getImmutable();//
        Element C3 = C3_.add(phi.mul(P).mul(b)).getImmutable();//
        Element C4 = C4_.add(phi.mul(Q).mul(b)).getImmutable();//
        for(int i = 0 ;i<attNum;i++ ){

            byte[] attHash1 = Tool.sha1(t);
            Element ht = bp.getG1().newElementFromHash(attHash1, 0, attHash1.length).getImmutable();
            temp.add(a.mul(ht));
        }
        Element Cy1 = a.mul(Si).getImmutable();//
        long end = System.currentTimeMillis();
        return end-start;
    }
    public static long Decrypt(int attNum){
        Pairing bp = PairingFactory.getPairing("a.properties");//

        long start = System.currentTimeMillis();





        long end = System.currentTimeMillis();
        return end-start;
    }

}
